package edu.ccut.saturn.studio.export.wizards;

import java.io.File;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;

import edu.ccut.saturn.studio.core.util.eclipse.SaturnEclipsePathUtil;
import edu.ccut.saturn.studio.core.util.file.SaturnFileUtils;
import edu.ccut.saturn.studio.export.createhtml.AnalyseProject;

/**
 * 项目开发文档向导类
 * 
 * @author 杨艳斌
 * 
 * @version 1.1 2009.12.11
 */

public class ExportDevelopmentWizard extends Wizard implements IExportWizard {

	private ExportDevelopmentWizardPage page;
	private ISelection selection;

	public void addPages() {														//添加向导页
		page = new ExportDevelopmentWizardPage(selection);
		addPage(page);
	}

	public boolean performFinish() {									//点击finish按钮所触发的事件
		
		try {
			String path = SaturnEclipsePathUtil.getEclipsePath();
			String srcpath = path.replace("SaturnStudio/ide/eclipse",	//获得发布版中edu.ccut.saturn.tools的路径
							"SaturnStudio/ide/saturn/eclipse/plugins/edu.ccut.saturn.tools");
			String toolspath = path + "edu.ccut.saturn.tools";			//获得开发时edu.ccut.saturn.tools的路径
			List<IProject> projects = page.getProjects();
			StringBuffer content = new StringBuffer();
			
			if (!new File(page.getSaveDirectory()).exists()) {
				(new File(page.getSaveDirectory())).mkdir();
			}
			
			//复制开发文档所需的样式和图片包edu.ccut.saturn.tools到指定位置
			if (srcpath.endsWith("tools/")) {
				SaturnFileUtils.copyFileFolder(srcpath, page.getSaveDirectory() + "/tools");
			}
			if (toolspath.endsWith("tools")) {
				SaturnFileUtils.copyFileFolder(toolspath, page.getSaveDirectory() + "/tools");
			}

			for (IProject project : projects) {
				AnalyseProject.CreateProjectFolderrDirectory(project, page
						.getSaveDirectory());									//创建文档生成的目录结构
				AnalyseProject.CreateProjectHtml(project, page
						.getSaveDirectory());									//创建HTML文档
				content.append(AnalyseProject.CreateProjectTree(project, page
						.getSaveDirectory()));									//添加单个工程对应的菜单树
			}
			
			AnalyseProject.CreateProjectsHtml(page.getSaveDirectory(),
					projects, content);									//创建多工程导出时的首页、导航树和工程列表

		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return true;
	}
	
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
}
